package mysql

import (
	"fmt"
	"furniture/appconfig"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var (
	DB  *gorm.DB
	err error
)

func Init() {
	MysqlConfig := appconfig.Config.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True",
		MysqlConfig.User,
		MysqlConfig.Password,
		MysqlConfig.Host,
		MysqlConfig.Port,
		MysqlConfig.Database,
	)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据库连接成功")

	err = DB.AutoMigrate(&Furniture{}, &Class{}, &Brand{})
	if err != nil {
		panic("数据表迁移失败")
	}
	fmt.Println("数据表迁移成功")
}
